import mongoose from '../mongo'

const userSchema: mongoose.Schema = new mongoose.Schema({
    name: { // 用户名
        type: String,
        required: true
    },
    avater: { // 头像
        type: String,
        default: '/avater.jpeg'
    },
    account: { // 账号
        type: String,
        required: true
    },
    password: { // 密码
        type: String,
        required: true
    },
    is_login: { // 是否登录
        type: Boolean,
        default: false
    },
    bio: { // 简介
        type: String,
        default: ''
    },
    space: [{ // 空间动态
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Space'
    }],
    linkman: [{ // 好友列表
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    }],
    group: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Group'
    }]
})

const spaceSchema: mongoose.Schema = new mongoose.Schema({
    user: { // 用户
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    },
    content: { // 内容
        type: String,
        default: ''
    },
    images: [{ // 图片
        type: String,
        default: []
    }],
    videos: [{
        type: String,
        default: []
    }],
    likes: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    }],
    comments: [{ // 评论
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Comment'
    }],
})

const commentSchema: mongoose.Schema = new mongoose.Schema({
    user: { // 用户
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    },
    content: { // 内容
        type: String,
        default: ''
    },
    space: { // 发布的空间
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Space'
    },
    video: { // 发布的视频
        type: String,
        default: ''
    },
})

const messageSchema = new mongoose.Schema({
    type: {
        type: String,
        enum: ['text', 'img', 'video'],
        require: true,
    },
    is_group: { // 是否是群聊，是那就是群聊
        type: Boolean,
        require: true,
    },
    group_name: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Group',
    },
    title: { // 文本消息
        type: String,
    },
    img: {
        type: String,
    },
    video: {
        type: String,
    },
    user: { // 发送者
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
    },
    to_user: { // 对方用户
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
    },
})

const groupSchema = new mongoose.Schema({
    title:{
        type: String,
        require: true,
    },
    user: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
    }],
    avater: {
        type: String,
    },
})

const User = mongoose.model('User', userSchema, 'user')
const Space = mongoose.model('Space', spaceSchema, 'space')
const Comment = mongoose.model('Comment', commentSchema, 'comment')
const Message = mongoose.model('Message', messageSchema, 'message')
const Group = mongoose.model('Group', groupSchema, 'group')

export {
    User,
    Space,
    Comment,
    Message,
    Group,
}